<?php

/**
 * Description of model_friends
 *
 * @author Steffen Bilde
 */
class model_friends extends CI_Model
{
    function  __construct()
    {
        parent::__construct();
    }


    //Creates a 'friend_request'-row by $sender_id and $recipient_id
    function request_friend($sender_id,$recipient_id)
    {
       $data = array(
                    'sender' => $sender_id,
                    'recipient' => $recipient_id);
        $this->db->insert('friend_requests',$data);
    }
    
    //Gets all 'friend_request's where 'recipient'-field = $id
    function waiting_requests($id)
    {

        $data = array();
        $this->db->where('recipient',$id);
        $Q = $this->db->get("friend_requests");

        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    //Creates a row in 'relationships'-table, and deletes original 'friend-request'
    function accept_friend_request($myid,$sender_id)
    {
        $data = array(

                    'user_ida' => $sender_id,
                    'user_idb' => $myid,
                    'type' => '1');
        $this->db->insert('relationships',$data);

        $this->delete_friend_request($myid,$sender_id);

        

    }

    //Delete a friend_request-ow from friend-request-table by $myid & $sender_id
    function delete_friend_request($myid,$sender_id)
    {
        $this->db->limit(1);
        $this->db->where('sender', $sender_id);
        $this->db->where('recipient', $myid);
	$this->db->delete('friend_requests');
    }

    //Delete 'relationship'-row by $myid & $friend_id
    //The double query is because, that you as user, could have been requested or had requested the user,
    //when you started the relationship.
    function remove_friend($myid,$friend_id)
    {        
        $sql = "DELETE FROM relationships WHERE user_ida = $myid AND user_idb = $friend_id";

        $sql2 = "DELETE FROM relationships WHERE user_ida = $friend_id AND user_idb = $myid";

        $this->db->query($sql);
        $this->db->query($sql2);
    }

    //Gets all 'friends' from 'user'-table by $id.
    function list_all_friends($id)
    {
        
        $sql = "SELECT * FROM user WHERE id in (SELECT user_ida as id FROM relationships WHERE user_idb = $id AND type = 1)"
             . " UNION "
             . "SELECT * FROM user WHERE id in (SELECT user_idb FROM relationships WHERE user_ida = $id AND type = 1)";

        $Q = $this->db->query($sql);

        if ($Q->num_rows() > 0)
        {
          foreach ($Q->result() as $row)
          {            
              $data[] = $row;
          }
        }

        else
        {
            $data = array();
        }
        $Q->free_result();

        return $data;

    }

    //get all friend-id's by 'logged_in_uder'-id ($id)
    function get_friends($id)
    {

        $sql = "(SELECT user_ida as id FROM relationships WHERE user_idb = $id AND type = 1)"
             . " UNION "
             . "(SELECT user_idb FROM relationships WHERE user_ida = $id AND type = 1)";
        $Q = $this->db->query($sql);


        if ($Q->num_rows() > 0)
        {
          foreach ($Q->result() as $row)
          {
              $data[] = $row->id;
          }
        }
        else
        {
            $data = array();
        }
        $Q->free_result();

        return $data;
    }
        
}

?>
